﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TradingServer.Core.Collections
{
	public class ConcurrentList<T> : IList<T>
	{
		#region Implementation of IEnumerable

		public IEnumerator<T> GetEnumerator()
		{
			lock (ThisLock) return _innerList.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		#endregion

		#region Implementation of ICollection<T>

		public void Add(T item)
		{
			lock (ThisLock) _innerList.Add(item);
		}

		public void Clear()
		{
			lock (ThisLock) _innerList.Clear();
		}

		public bool Contains(T item)
		{
			lock (ThisLock) return _innerList.Contains(item);
		}

		public void CopyTo(T[] array, int arrayIndex)
		{
			lock (ThisLock) _innerList.CopyTo(array, arrayIndex);
		}

		public bool Remove(T item)
		{
			lock (ThisLock) return _innerList.Remove(item);
		}

		public int Count
		{
			get { lock (ThisLock) return _innerList.Count; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		#endregion

		#region Implementation of IList<T>

		public int IndexOf(T item)
		{
			lock (ThisLock) return _innerList.IndexOf(item);
		}

		public void Insert(int index, T item)
		{
			lock (ThisLock) _innerList.Insert(index, item);
		}

		public void RemoveAt(int index)
		{
			lock (ThisLock) _innerList.RemoveAt(index);
		}

		public T this[int index]
		{
			get { lock (ThisLock) return _innerList[index]; }
			set { lock (ThisLock) _innerList[index] = value; }
		}

		#endregion

		#region Private members

		private readonly List<T> _innerList = new List<T>();
		private readonly object ThisLock = new object();

		#endregion
	}
}
